﻿using System;
using System.Collections.Generic;

namespace SeaBattle.Domain.Entities
{
    public enum GameState
    {        
        STATE_CREATED = 0,
        STATE_GAME_SETUP,
        STATE_OPENED,
        STATE_BOARD_SETUP,
        STATE_PLAY,
        STATE_PAUSE,
        STATE_CLOSED
    }

    public class Game
    {
        public int Id { get; private set; }
        public int Preset { get; set; }
        public DateTime Created { get; private set; }
        public Player Creator { get; private set; }
        public GameState State { get; private set; }
        private Player opponent;
        public Player Opponent { get { return opponent; } set { SetOpponent(value); } }
        public Dictionary<Player, Fleet> Navies { get; set; }
        

        public Game(int id, DateTime created, Player creator)
        {
            Id = id;
            Created = created;
            Creator = creator;
            State = GameState.STATE_CREATED;
        }

        private void InitializeFleet(Player player) {}


        public bool Opened
        {
            get { return State == GameState.STATE_OPENED; }
            set { if (value) State = GameState.STATE_OPENED; }
        }

        public bool Closet
        {
            get { return State == GameState.STATE_CLOSED; }
            set { if (value) State = GameState.STATE_CLOSED; }
        }

        public bool BoardPreparing
        {
            get { return State == GameState.STATE_BOARD_SETUP; }
            set { if (value) State = GameState.STATE_BOARD_SETUP; }
        }

        private void SetOpponent(Player theOpponent)
        {
            // The game must be in Opened status
            if (!Opened)
            {
                throw new Exception("The game " + Id + " not opened");
            }
            // Check whether Creator and Opponent are not the same persons
            if (Creator == theOpponent)
            {
                throw new Exception("The opponent is the same as creator of the game " + Id);
            }
            // Set opponent
            opponent = theOpponent;
        }

        public void JoinOpponent(Player theOpponent)
        {
            Opponent = theOpponent;
            BoardPreparing = true;
        }

    }
}
